package com.example.service;

import com.example.entity.Business;
import com.example.exception.CustomException;
import com.example.mapper.BusinessMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessService {
    @Resource
    private BusinessMapper businessMapper;

    public List<Business> selectAll(Business business) {
        return businessMapper.selectAll(business);

    }

    public Business login(Business business) {
        String username = business.getUsername();
        Business match_business = businessMapper.selectByUsername(username);
        if (match_business == null) {
            throw new CustomException("用户名不存在", 500);
        } else {
            String password = match_business.getPassword();
            if (!password.equals(business.getPassword())) {
                throw new CustomException("账号或密码错误", 500);
            }
        }
        return match_business;
    }

    public void register(Business business) {
        String username = business.getUsername();
        String shopname=business.getShopname();
        Business match_business = businessMapper.selectByName(username,shopname);
        if (match_business != null) {
            throw new CustomException("用户名已存在", 500);
        } else {
            business.setAvatar("");
            businessMapper.addBusiness(business);
        }
    }

    public void update(Business business) {
        businessMapper.updateById(business);
    }

    public Business selectById(Integer id) {
        return businessMapper.selectById(id);
    }
}
